package Dao;

import Bean.Contact;
import Bean.Join;
import Bean.Out;
import Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OutDao {

    public static List<Out> getOutList(){
        List<Out> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "select * from tb_outdepot";
            //执行sql
            pst = con.prepareStatement(sql);
            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
               Out out = new Out();
                out.setId(rs.getInt(1));
                out.setDid(rs.getInt(2));
                out.setName(rs.getString(3));
                out.setDepotTime(rs.getString(4));
                out.setWeight(rs.getFloat(5));
                out.setRemark(rs.getString(6));
                list.add(out);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return list;
    }

    public static List<Out> queryInfoByDid(String[] where){
        List<Out> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "";
        try {
            con = JDBCUtils.getConnection();
                if("".equals(where[1])){
                    sql = "select * from tb_outdepot where did = ?";
                    pst = con.prepareStatement(sql);
                    pst.setString(1,where[0]);
                }else {
                    sql  = "select * from tb_outdepot where did = ? and outTime like ?";
                    pst = con.prepareStatement(sql);
                    pst.setString(1,where[0]);
                    pst.setString(2,"%"+where[1]+"%");
                }

            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                Out o = new Out();
                o.setId(rs.getInt(1));
                o.setDid(rs.getInt(2));
                o.setName(rs.getString(3));
                o.setDepotTime(rs.getString(4));
                o.setWeight(rs.getFloat(5));
                o.setRemark(rs.getString(6));
                list.add(o);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return list;
    }

    public static boolean addInfo(Out out){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "insert into tb_outdepot value(?,?,?,?,?,?)";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,out.getId());
            pst.setInt(2,out.getDid());
            pst.setString(3,out.getName());
            pst.setString(4,out.getDepotTime());
            pst.setFloat(5,out.getWeight());
            pst.setString(6,out.getRemark());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  false;
    }

    public static List<Integer> findId(){
        List<Integer> idList = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "select id from tb_outDepot";
            //执行sql
            pst = con.prepareStatement(sql);

            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                idList.add(rs.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return idList;
    }

public static boolean updateOutInfo(Out o){
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    try {
        con = JDBCUtils.getConnection();
        String sql = "update  tb_outDepot set did = ?,wareName = ?,outTime = ?,weight = ?,remark = ? where id = ?";
        //执行sql
        pst = con.prepareStatement(sql);
        //设置参数
        pst.setInt(1,o.getDid());
        pst.setString(2,o.getName());
        pst.setString(3,o.getDepotTime());
        pst.setFloat(4,o.getWeight());
        pst.setString(5,o.getRemark());
        pst.setInt(6,o.getId());
        //接收结果
        int i = pst.executeUpdate();
        if(i>0){
            return true;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        JDBCUtils.close(rs,pst,con);
    }
    return  false;
}
    public static boolean deleteOutInfo(Out o){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "delete from tb_outDepot where id = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,o.getId());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  false;
    }
}
